package com.lhy.tool.patchplugin.util;

import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * 文件工具类
 */
public class FileUtils {

    FileUtils() {

    }

    private static final Logger LOG = LoggerFactory.getLogger(FileUtils.class);


    /**
     * 读取classpath下文件内容
     *
     * @param classpath
     * @return
     */
    public static String readResource(String classpath) {
        try {
            return getResourceSteam(classpath);
        } catch (IOException e) {
            LOG.error("读取资源文件出错：" + classpath, e);
        }
        return "";
    }

    public static String getResourcePath(String classpath) {
        File resource = getResource(classpath);
        if (resource == null) {
            LOG.error("未获取到资源文件：" + classpath);
            return "";
        }
        return resource.getAbsolutePath();
    }

    private static File getResource(String classpath) {
        File f = null;
        try {
            URL resource = FileUtils.class.getClassLoader().getResource(classpath);
            f = new File(resource.toURI());
        } catch (Exception e) {
            LOG.error("读取资源文件出错：" + classpath, e);
        }
        return f;
    }

    /**
     * @param classpath
     * @return
     * @throws IOException
     * @Title: getResourceSteam
     * @Description: 可以获取jar包中文件
     */
    private static String getResourceSteam(String classpath) throws IOException {
        InputStream resourceAsStream = FileUtils.class.getClassLoader().getResourceAsStream(classpath);
        String string = IOUtils.toString(resourceAsStream, Charsets.toCharset("utf-8"));
        if (resourceAsStream != null) {
            resourceAsStream.close();
        }
        return string;
    }

    /**
     * @param classpath
     * @param file
     * @return void 返回类型
     * @throws IOException
     * @Title: copyFile
     * @Description: 从jar包或class路径复制文件
     */
    public static void copyFileFromResource(String classpath, File file) throws RuntimeException {
        InputStream resourceAsStream = null;
        try {
            if (file.exists()) {
                file.delete();
            }
            File pFile = file.getParentFile();
            if (!pFile.exists()) {
                pFile.mkdirs();
            }
            file.createNewFile();
            File resource = getResource(classpath);
            if (resource != null) {
                org.apache.commons.io.FileUtils.copyFile(resource, file);
            } else {
                resourceAsStream = FileUtils.class.getClassLoader().getResourceAsStream(classpath);
                byte[] tempbytes = new byte[1024];
                @SuppressWarnings("unused")
                int byteread = 0;
                while ((byteread = resourceAsStream.read(tempbytes)) != -1) {
                    org.apache.commons.io.FileUtils.writeByteArrayToFile(file, tempbytes, true);
                }
            }
        } catch (IOException e) {
            LOG.error("文件复制失败：{}", classpath, e);
        } finally {
            if (resourceAsStream != null) {
                try {
                    resourceAsStream.close();
                } catch (IOException e) {
                    LOG.error("关闭线程异常", e);
                }
            }
        }
    }

    /**
     * @param fileName
     * @return
     * @throws Exception
     * @Function: FileUtil::getFileType
     * @Description: 获取文件后缀名
     * @version: v1.0.0
     * @author: luanhy
     * @date: 2015-2-6 下午03:03:36
     * <p>
     * Modification History: Date Author Version Description
     * -------------------------------------------------------------
     */
    public static String getFileType(String fileName) {
        String[] split = fileName.split("\\.");
        String fileType = "";
        if (split.length > 1) {
            fileType = split[split.length - 1];
        } else {
            fileType = fileName;
        }
        return fileType;
    }

    public static String getFileName(String fileName) {
        String[] split = fileName.split("\\.");
        StringBuilder fileNamePix = new StringBuilder();
        if (split.length > 1) {
            for (int i = 0; i < split.length - 1; i++) {
                fileNamePix.append(split[i]);
                if (i < split.length - 2) {
                    fileNamePix.append(".");
                }
            }
        } else {
            fileNamePix.append(fileName);
        }
        return fileNamePix.toString();
    }
}
